﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebApplication1
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                LoadProizvoditeli();
            }
        }

        protected void LoadProizvoditeli()
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT ImeProizvoditel FROM proizvoditeli";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["ImeProizvoditel"].ToString();
                    element.Value = citac["ImeProizvoditel"].ToString();
                    ProizvoditeliListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                // Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadModeli(String Marka)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT Model FROM modeli WHERE Proizvoditel='" + Marka + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["Model"].ToString();
                    element.Value = citac["Model"].ToString();
                    ModeliListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
             //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadMotori(String Model)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT Broj, Motor FROM motori WHERE Model='" + Model + "' ORDER BY Gorivo, Motor";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["Motor"].ToString();
                    element.Value = citac["Broj"].ToString();
                    MotoriListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //  Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadMotorInfo(String ID)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM motori WHERE Broj='" + ID + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                if (citac.Read())
                {
                    MotorBroj.Text = citac["Broj"].ToString();
                    MotorModel.Text = citac["Model"].ToString();
                    MotorID.Text = citac["Motor"].ToString();
                    MotorSila.Text = citac["SilaKS"].ToString();
                    MotorGorivo.Text = citac["Gorivo"].ToString();
                    MotorZafatnina.Text = citac["Zafatnina"].ToString();
                    MotorPotrosuvacka.Text = citac["Potrosuvacka"].ToString();
                    MotorCena.Text = citac["CenaEvra"].ToString();
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //  Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void ProizvoditeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModeliListBox.Items.Clear();
            MotoriListBox.Items.Clear();
            MotorBroj.Text = MotorModel.Text = MotorID.Text = MotorSila.Text = MotorGorivo.Text = MotorZafatnina.Text = MotorPotrosuvacka.Text = MotorCena.Text = "";
            LoadModeli(ProizvoditeliListBox.SelectedValue);
        }

        protected void ModeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MotoriListBox.Items.Clear();
            MotorBroj.Text = MotorModel.Text = MotorID.Text = MotorSila.Text = MotorGorivo.Text = MotorZafatnina.Text = MotorPotrosuvacka.Text = MotorCena.Text = "";
            LoadMotori(ModeliListBox.SelectedValue);
        }

        protected void MotoriListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMotorInfo(MotoriListBox.SelectedValue);
        }

        protected void ButtonIzbrisi_Click(object sender, EventArgs e)
        {
            DataBase.IzbrisiMotor(MotorBroj.Text);
            MotoriListBox.Items.Clear();
            LoadMotori(ModeliListBox.SelectedValue);
        }

        protected void ButtonPromeni_Click(object sender, EventArgs e)
        {
            int poz = MotoriListBox.SelectedIndex;
            DataBase.AzurirajMotor(MotorBroj.Text, MotorModel.Text, MotorID.Text, MotorSila.Text, MotorGorivo.Text, MotorZafatnina.Text, MotorPotrosuvacka.Text, MotorCena.Text);
            MotoriListBox.Items.Clear();
            MotoriListBox.SelectedIndex = poz;
        }

        protected void ButtonVnesi_Click(object sender, EventArgs e)
        {
            DataBase.VnesiMotor(MotorModel.Text, MotorID.Text, MotorSila.Text, MotorGorivo.Text, MotorZafatnina.Text, MotorPotrosuvacka.Text, MotorCena.Text);
            MotoriListBox.Items.Clear();
            LoadMotori(ModeliListBox.SelectedValue);
        }
    }
}
